import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @author 帅小伙
 * @date 2022/1/3
 * @description  手动消息应答
 */
public class Consumer1 {

    private static final String QUEUE_NAME = "hello";
    public void consume( ) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        System.out.println(Thread.currentThread().getName()+"  消费者启动等待消费.........");

        String name = Thread.currentThread().getName();
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody());
            System.out.println(name+"接收到的消息"+message);
            // 类似快递编号
            System.out.println(delivery.getEnvelope().getDeliveryTag());
            // 三个手动应答方式
            //channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
            //channel.basicNack(delivery.getEnvelope().getDeliveryTag(),false,false);
            channel.basicReject(delivery.getEnvelope().getDeliveryTag(),false);
        };

        // 手动签收
        channel.basicConsume(QUEUE_NAME, false, deliverCallback, (consumerTag)->{
            System.out.println(consumerTag+"消费者取消消费信息");
        });
        //
    }
}
